﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eTerm.MsSql.DbProvider;
using eTerm.Platform.Common;

namespace eTerm.Platform.Web.PNR
{
    public partial class DetrSync : BasePage
    {
        protected void btnQuery_Click(object sender, EventArgs e) {
            AspNetPager1.CurrentPageIndex = 1;
            QueryData();
            //btnQuery.Visible = false;
            btnImport.Visible = true;
        }

        protected override void InitEasyUI()
        {
            BaseRegisterStartupScript<UpdatePanel>(GetUpdatePanel(@"pnlContent") as UpdatePanel, btnQuery.ClientID, string.Format(@"$('#{0}').linkbutton({{   
    iconCls: 'icon-search'  
}});  
", btnQuery.ClientID), true);

            BaseRegisterStartupScript<UpdatePanel>(GetUpdatePanel(@"pnlContent") as UpdatePanel, btnImport.ClientID, string.Format(@"$('#{0}').linkbutton({{   
    iconCls: 'icon-laptop_disk'  
}});  
", btnImport.ClientID), true);


            BaseRegisterStartupScript<UpdatePanel>(GetUpdatePanel(@"pnlContent") as UpdatePanel, tbEndDate.ClientID, string.Format(@"$('#{0}').datebox();  
", tbEndDate.ClientID), true);

            BaseRegisterStartupScript<UpdatePanel>(GetUpdatePanel(@"pnlContent") as UpdatePanel, tbStartDate.ClientID, string.Format(@"$('#{0}').datebox();  
", tbStartDate.ClientID), true);

            BaseRegisterStartupScript<UpdatePanel>(GetUpdatePanel(@"pnlContent") as UpdatePanel, dlUploader.ClientID, string.Format(@"$('#{0}').combobox();  
", dlUploader.ClientID), true);

            
        }




        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            QueryData();
        }


        protected override void InitPageLoad()
        {
            tbStartDate.Text = DateTime.Now.ToString(@"yyyy-MM-dd");

            dlUploader.DataSource = new List<SysUser>() { new SysUser() { UserId = 0, DisplayName = @"  全  部  " } }.Union(SysUser.All().Where(l => l.CompanyCode == CurrentUser.CompanyCode).ToList());
            //SysUser.All().Where(l => l.CompanyCode == CurrentUser.CompanyCode).ToList().ForEach(l => {
            //    dlUploader.Items.Add(new ListItem(l.DisplayName, l.UserId.ToString()));
            //});

            dlUploader.DataTextField = @"DisplayName";
            dlUploader.DataValueField = @"UserId";
            dlUploader.DataBind();
        }


        private void QueryData()
        {
            int PageID = AspNetPager1.CurrentPageIndex;
            int PageSize = AspNetPager1.PageSize;
            //int uid = int.Parse(dlUploader.SelectedValue);
            //chkStatus.Items.Cast<ListItem>().Where(l => l.Selected).ToList().ForEach(l =>
            //{
            //    Status = Status | int.Parse(l.Value);
            //});

            var sql = VErpTicketNo.All().Join(SysUser.All(), l => l.UserId, r => r.UserId, (l, r) => new { l.Id, l.TicketDate, l.TicketNo, l.TradeDate, l.TradeNo, l.TradeType, l.UserId, l.UserNo, r.DisplayName, ProcessDate = DateTime.Now }).Where(l => l.TradeType == @"出票");//.Select(l=> new PnrDetr() { UserId = l.UserId, ProcessDate =DateTime.Now, StatusId =(int)DetrStatus.NONE, TicketDate = l.TicketDate,  });


            if (dlUploader.SelectedValue != "0")
                sql = sql.Where(l => l.UserId == int.Parse(dlUploader.SelectedValue));
            else
                ;// sql = sql.Where(l => l.UserId == UID);

            if (!string.IsNullOrEmpty(tbStartDate.Text.Trim()))
                sql = sql.Where(l => l.TradeDate >= DateTime.Parse(tbStartDate.Text.Trim()));

            if (!string.IsNullOrEmpty(tbEndDate.Text.Trim()))
                sql = sql.Where(l => l.TradeDate <= DateTime.Parse(tbEndDate.Text.Trim()).AddDays(1));

            this.AspNetPager1.RecordCount = sql.Count();
            this.rptDetr.DataSource = sql
                .Skip((PageID - 1) * PageSize)
                .Take(PageSize)
                .OrderByDescending(l => l.TradeDate).ToList()
            ;

            this.AspNetPager1.CustomInfoHTML = string.Format("当前第{0}/{1}页 共{2}条记录 每页{3}条", new object[] { this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageCount, this.AspNetPager1.RecordCount, this.AspNetPager1.PageSize });

            this.rptDetr.DataBind();
            this.AspNetPager1.DataBind();
        }


        protected void btnImport_Click(object sender, EventArgs e) {
            //btnQuery.Visible = true;
            btnImport.Visible = false;
            var sql = VErpTicketNo.All().Join(SysUser.All(), l => l.UserId, r => r.UserId, (l, r) => new { l.Id, l.TicketDate, l.TicketNo, l.TradeDate, l.TradeNo, l.TradeType, l.UserId, l.UserNo, r.DisplayName }).Where(l => l.TradeType == @"出票");//.Select(l=> new PnrDetr() { UserId = l.UserId, ProcessDate =DateTime.Now, StatusId =(int)DetrStatus.NONE, TicketDate = l.TicketDate,  });


            if (dlUploader.SelectedValue != "0")
                sql = sql.Where(l => l.UserId == int.Parse(dlUploader.SelectedValue));
            else
                ;// sql = sql.Where(l => l.UserId == UID);

            if (!string.IsNullOrEmpty(tbStartDate.Text.Trim()))
                sql = sql.Where(l => l.TradeDate >= DateTime.Parse(tbStartDate.Text.Trim()));

            if (!string.IsNullOrEmpty(tbEndDate.Text.Trim()))
                sql = sql.Where(l => l.TradeDate <= DateTime.Parse(tbEndDate.Text.Trim()).AddDays(1));

            sql.ToList().ForEach(l =>
            {
                new PnrDetr() { 
                 TicketDate = l.TicketDate,
                  StatusId = (int)DetrStatus.PENDDING,
                   UserId = l.UserId,
                    ProcessDate = DateTime.Now,
                     TicketNo =l.TicketNo
                }.Save();
            });
        }


        protected void rptDetr_OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
//                LinkButton lnkHistory = e.Item.FindControl(@"lnkHistory") as LinkButton;
//                LinkButton lnkComplete = e.Item.FindControl(@"lnkComplete") as LinkButton;
                //Literal litStatusName = e.Item.FindControl("litStatusName") as Literal;

                //PnrDetr detr = e.Item.DataItem as PnrDetr;

//                lnkComplete.Visible = detr.UserId == UID && detr.StatusId != (int)DetrStatus.COMPLETED;
//                lnkHistory.Visible = detr.StatusId != (int)DetrStatus.NONE;
//                BaseRegisterStartupScript<UpdatePanel>(GetUpdatePanel(@"pnlContent") as UpdatePanel, lnkHistory.ClientID, string.Format(@"$('#{0}').linkbutton({{   
//    iconCls: 'icon-tip'  
//}});  
//", lnkHistory.ClientID), true);

//                BaseRegisterStartupScript<UpdatePanel>(GetUpdatePanel(@"pnlContent") as UpdatePanel, lnkComplete.ClientID, string.Format(@"$('#{0}').linkbutton({{   
//    iconCls: 'icon-save'  
//}});  
//", lnkComplete.ClientID), true);

                //litStatusName.Text = GetDescription((DetrStatus)detr.StatusId);
//                lnkHistory.OnClientClick = string.Format(@"$.jBox('post:PNRHistory.aspx', {{ajaxData: {{ 'ID':'{0}' }},title:'票号历史信息',width:650,   bottomText: '票号处理历史记录' }});return false;", detr.Id);

//                lnkComplete.OnClientClick = string.Format(@"$.jBox.confirm('确实需要手工完成票号处理？', '票号处理', function(v,h,f){{if(v=='ok'){{ $.post('../ISAPI/SysUserAPI.ashx',{{ ID:{0},Action:'completedetr' }},function(){{ $.jBox.tip('票号 {1} 手工处理成功。', 'success'); $('#{2}').hide(); }}); }}else{{  $.jBox.tip('票号 {1} 手工处理操作已取消。', 'info'); }} }},{{ bottomText: '处理完成后系统将不再自动搜索票号信息' }}); return false;", detr.Id, detr.TicketNo, lnkComplete.ClientID);

            }
        }


    }
}
